﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;

namespace GUI
{
    public partial class QuanLyDonHang : Form
    {
        public QuanLyDonHang()
        {
            InitializeComponent();
        }

        private bool boolTu = false;
        private bool boolDen = false;

        private void Search()
        {
            DateTime dateTu = new DateTime();
            DateTime dateDen = new DateTime();
            DateTime dateHienTai = new DateTime();
            if (boolTu)
            {
                dateTu = dateTimePicker_ngayBatDau.Value.Date;
            }
            else
            {
                dateTu = new DateTime(1, 1, 1).Date;
            }

            if (boolDen)
            {
                dateDen = dateTimePicker_ngayKetThuc.Value.Date;
            }
            else
            {
                dateDen = DateTime.Now.Date;
            }

            int vt = -1;
            for (int i = dataGridView_quanLyDonHang.RowCount - 1; i >= 0; i--)
            {                
                dataGridView_quanLyDonHang.Rows[i].Visible = false;
                if (dataGridView_quanLyDonHang.Rows[i].Cells["clMaPD"].Value.ToString().ToUpper().IndexOf(txt_maPD.Text.ToString().ToUpper()) >= 0 && dataGridView_quanLyDonHang.Rows[i].Cells["clNhanVien"].Value.ToString().ToUpper().IndexOf(txt_tenNV.Text.ToString().ToUpper()) >= 0 && dataGridView_quanLyDonHang.Rows[i].Cells["clThanhTien"].Value.ToString().CompareTo(txt_tongTien.Text.ToString()) >= 0)
                {
                    vt = i;
                    dataGridView_quanLyDonHang.Rows[i].Visible = true;
                }
            }

            if (vt != -1)
            {
                dataGridView_quanLyDonHang.CurrentCell = dataGridView_quanLyDonHang.Rows[vt].Cells[clMaPD.Index];
                dataGridView_quanLyDonHang.CurrentCell.Selected = true;                
            }            
        }

        private void btn_capNhat_Click(object sender, EventArgs e)
        {

        }

        private void QuanLyDonHang_Load(object sender, EventArgs e)
        {
            int TongPhieu = 0;
            DataTable dataTable = PhieuDatBUS.SelectPhieuDatAll(ref TongPhieu);
            //nhap
            //MessageBox.Show("Vui lòng chờ trong giây lát...");
            int stt = 1;

            foreach (DataRow item in dataTable.Rows)
            {
                dataGridView_quanLyDonHang.Rows.Add(stt, item["Ma_PD"].ToString(), item["NV_Dat"].ToString(), item["Ten_KH"].ToString(), item["DiaChi"].ToString(), item["DienThoai"].ToString(), item["NgayDat"].ToString(), item["ThanhTien"].ToString(), item["TinhTrang"].ToString(), item["GhiChu"].ToString());
                stt++;
            }

            txt_TongPhieu.Text = TongPhieu.ToString();
        }

        private void txt_maPD_TextChanged(object sender, EventArgs e)
        {
            Search();
        }

        private void txt_tenNV_TextChanged(object sender, EventArgs e)
        {
            Search();
        }

        private void txt_tongTien_TextChanged(object sender, EventArgs e)
        {
            Search();
        }

        private void btn_lamLai_Click(object sender, EventArgs e)
        {
            txt_tenNV.Text = txt_maPD.Text = txt_tongTien.Text = null;
        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
